package com.zjitc.utils;

import com.zjitc.model.ChatItem;
import org.omg.PortableInterceptor.INACTIVE;
import sun.misc.BASE64Encoder;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Administrator
 */
public class
Utils {

    public static String getUUID() {
        //随机生成UUID
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static <T> JsonTypeData<T> getDate(Object... obj) {
        int a = 3, b = 2;
        if (obj.length == a) {
            return new JsonTypeData<T>((Integer) obj[0], (String) obj[1], (T) obj[2]);
        } else if (obj.length == b && obj[1] instanceof String) {
            //status,msg
            return new JsonTypeData<T>((Integer) obj[0], (String) obj[1]);
        } else {
            //status,data
            return new JsonTypeData<T>((Integer) obj[0], (T) obj[1]);
        }
    }

    public static void main(String[] args)
            throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String s = encoderByMd5("1adfa");
        String s1 = encoderByMd5("1adfa");
        System.out.println(s1);
        System.out.println(s);
    }

    public static String encoderByMd5(String str)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        //确定计算方法
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
        String result = base64en.encode(md5.digest(str.getBytes("utf-8")));
        return result;
    }

    public static String dateToStr(Date date) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = format.format(date);
        return str;
    }

    /**
     * 字符串转换成日期
     *
     * @return date
     */
    public static Date strToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static Integer productCount(HttpServletRequest request, String pid, int count) {
        /**
         * 获取每一个秒杀商品的已经秒杀数量
         */
        ServletContext context = request.getServletContext();
        Integer productCount = null;
//        Synchronized  同步机制，
        synchronized (context) {
            Map<String, Integer> productMap =
                    (Map<String, Integer>) context.getAttribute("productSkill") == null ?
                            Collections.synchronizedMap(new LinkedHashMap<String, Integer>())
                            : (Map<String, Integer>) context.getAttribute("productSkill");
            productCount = productMap.get(pid);
            if (null == productCount) {
                productCount = count;
                productMap.put(pid, productCount);
            } else {
                productCount = productCount + count;
                productMap.put(pid, productCount);
            }
            context.setAttribute("productSkill", productMap);
        }
        return productCount;
    }
}
